package com.lms.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lms.domain.Project;
import com.lms.domain.Role;
import com.lms.domain.Training;
import com.lms.domain.User;
import com.lms.dto.UserDto;
import com.lms.dto.UserListDto;
import com.lms.dto.UserMapper;
import com.lms.service.UserService;

@Controller
public class RoleController extends MultiActionController {
	
	@Autowired
	private UserService service;

	@RequestMapping
	public String getUsersPage() {
	return "users";
	}

	@RequestMapping(value="/records")
	public @ResponseBody UserListDto getUsers() {
		System.out.println("getUsers..");

	UserListDto userListDto = new UserListDto();
	userListDto.setUsers(UserMapper.map(service.readAll()));
	return userListDto;
	}

	@RequestMapping(value="/get")
	public @ResponseBody User get(@RequestBody User user) {
	return service.read(user);
	}

	@RequestMapping(value="/create", method=RequestMethod.POST)
	public @ResponseBody UserDto create(
	@RequestParam String username,
	@RequestParam String password,
	@RequestParam String firstName,
	@RequestParam String lastName,
	@RequestParam Integer role,
	@RequestParam Integer project,
	@RequestParam Integer training) {
		
		System.out.println("create............");

		try{
			Role newRole = new Role();
			newRole.setRole(role);

			Project newProject = new Project();
			newProject.setProject(project);
			
			Training newTraining = new Training();
			newTraining.setTraining(training);

			User newUser = new User();
			newUser.setUsername(username);
			newUser.setPassword(password);
			newUser.setFirstName(firstName);
			newUser.setLastName(lastName);
			newUser.setRole(newRole);
			newUser.setProject(newProject);
			newUser.setTraining(newTraining);

			return UserMapper.map(service.create(newUser));
		}
		catch(Exception e){
			return null;
		}
	}

	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody UserDto update(
	@RequestParam String username,
	@RequestParam String firstName,
	@RequestParam String lastName,
	@RequestParam Integer role,
	@RequestParam Integer project,
	@RequestParam Integer training) {

	Role existingRole = new Role();
	existingRole.setRole(role);

	Project existingProject = new Project();
	existingProject.setProject(project);
	
	Training existingTraining = new Training();
	existingTraining.setTraining(training);

	User existingUser = new User();
	existingUser.setUsername(username);
	existingUser.setFirstName(firstName);
	existingUser.setLastName(lastName);
	existingUser.setRole(existingRole);
	existingUser.setProject(existingProject);
	existingUser.setTraining(existingTraining);

	return UserMapper.map(service.update(existingUser));
	}

	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody Boolean delete(
	@RequestParam String username) {

	User existingUser = new User();
	existingUser.setUsername(username);

	return service.delete(existingUser);
	}

	
	
}
